using UnityEngine;
using System.Collections;

public class AIBase : CanTakeDamage 
{
	public enum AttackType {Sniper=1, Always=2, Never=3, Rando=4}
	public AttackType attackType;
	
	public Motor mMotor;
	public GameObject Bullet;

    public float FireTimeout = .75f;
	public float FireTimer = 0;
	public int CurrencyDrop = 50;
	
	private float offSetX = 270;
	private float offSetY = 0;
	private float offSetZ = 0;
    private Transform Wave;
    private Manager.DifficultyClass difficultyManager = null;
    private EnemyDifficultyManager difficulty;
		public GameObject explosion;
	//public GameObject explosion2;
	
	protected override void Start ()
	{
        Wave = GameObject.FindGameObjectWithTag("WaveManager").transform;
        foreach (Transform child in Wave)
        {
            if (child.gameObject.activeInHierarchy)
            {
                difficultyManager = child.gameObject.GetComponent<Manager>().difficulty;
                difficulty = child.gameObject.GetComponent<EnemyDifficultyManager>();
            }
        }
        //Difficulty Health, Fire Rate, Currency
        difficulty.Add(Health, difficulty.Health.Adder, out Health);
        difficulty.AddPercentage(Health, difficulty.Health.PercentageMultiplier, out Health);
        if (difficulty.Health.EqualToEnabled)
        {
            Health = Mathf.FloorToInt(difficulty.Health.EqualTo);
        }
        difficulty.Add(FireTimeout, difficulty.FireRateInSeconds.Adder, out FireTimeout);
        difficulty.AddPercentage(FireTimeout, difficulty.FireRateInSeconds.PercentageMultiplier, out FireTimeout);
        if (difficulty.FireRateInSeconds.EqualToEnabled)
        {
            FireTimeout = difficulty.FireRateInSeconds.EqualTo;
        }

        difficulty.Add(CurrencyDrop, difficulty.CurrencyDrop.Adder, out CurrencyDrop);
        difficulty.AddPercentage(CurrencyDrop, difficulty.CurrencyDrop.PercentageMultiplier, out CurrencyDrop);
        if (difficulty.CurrencyDrop.EqualToEnabled)
        {
            CurrencyDrop = (int) Mathf.Floor(difficulty.CurrencyDrop.EqualTo);
        }

        var BulletDamage = Bullet.GetComponent<DealsDamage>();
        difficulty.Add(BulletDamage.DamageOnCollision, difficulty.BulletDamageDealt.Adder, out BulletDamage.DamageOnCollision);
        difficulty.AddPercentage(BulletDamage.DamageOnCollision, difficulty.BulletDamageDealt.PercentageMultiplier, out BulletDamage.DamageOnCollision);
        if (difficulty.BulletDamageDealt.EqualToEnabled)
        {
            BulletDamage.DamageOnCollision = (int) Mathf.Floor(difficulty.BulletDamageDealt.EqualTo);
        }

        var Motor = GetComponent<Motor>();

        difficulty.Add(Motor.VerticalSpeed, difficulty.VerticalSpeed.Adder, out Motor.VerticalSpeed);
        difficulty.AddPercentage(Motor.VerticalSpeed, difficulty.VerticalSpeed.PercentageMultiplier, out Motor.VerticalSpeed);
        if (difficulty.VerticalSpeed.EqualToEnabled)
        {
            Motor.VerticalSpeed = difficulty.VerticalSpeed.EqualTo;
        }

        difficulty.Add(Motor.HorizontalSpeed, difficulty.HorizontalSpeed.Adder, out Motor.HorizontalSpeed);
        difficulty.AddPercentage(Motor.HorizontalSpeed, difficulty.HorizontalSpeed.PercentageMultiplier, out Motor.HorizontalSpeed);
        if (difficulty.HorizontalSpeed.EqualToEnabled)
        {
            Motor.HorizontalSpeed = difficulty.HorizontalSpeed.EqualTo;
        }




        base.Start ();
		FireTimer = FireTimeout;
		if(attackType == AttackType.Rando)
		{
			attackType = (AttackType) Random.Range(1,4);
		}

	}

    public override void TakeDamage(int dmg)
    {
        ToggleShader();
        base.TakeDamage(dmg);
        Invoke("ToggleShader", .2f);
    }
    public void ToggleShader()
    {
        
        if (renderer.material.shader == Shader.Find("Transparent/Diffuse") || renderer.material.shader == Shader.Find("Transparent/Bumped Diffuse"))
        {
            //renderer.material.color = Color.red;
            renderer.material.shader = Shader.Find("Particles/Additive");
            
        }
        else if (renderer.material.shader == Shader.Find("Particles/Additive"))
        {
            //renderer.material.color = Color.white;
            renderer.material.shader = Shader.Find("Transparent/Diffuse");
        }
    }
	protected virtual void Update()
	{
		mMotor.Move();
		Attack();
		FireTimer += Time.deltaTime;
	}
//    public override void TakeDamage(int dmg)
//    {
//        //Debug.Log("DAMNASDFhSLSFDHsfd");
//
//        base.TakeDamage(dmg);
//    }

	protected virtual void Attack()
	{
		switch(attackType)
		{
		case AttackType.Always:
                Fire();
			break;
		case AttackType.Sniper:
			var hit = Physics.RaycastAll(transform.position, Vector3.down);
			foreach(var h in hit)
			{
				if(h.collider.gameObject.tag == "Player")
				{
                    Fire();
					break;
				}
			}
			break;
		default:
			break;
		}
	}
	
	void OnDestroy()
	{
		var p = Spawner.Instance();
		if(p)
			p.ShipDead();
	}
	
	public virtual void Fire()
	{
		if(FireTimer >= FireTimeout)
		{
			FireTimer = 0;
			Weapon b = ((GameObject)Instantiate(Bullet,transform.position,Quaternion.Euler(offSetX,offSetY,offSetZ))).GetComponent<Weapon>();
			b.fireDirection = Vector3.Cross(new Vector3(offSetX,offSetY,offSetZ), Vector3.down).normalized;
			//b.gameObject.layer = gameObject.layer;
		}
	}
	public override void Die()
	{
		//Debug.Log("create explosion here");
		GameObject burst = (GameObject) Instantiate( explosion,transform.position,Quaternion.identity);
		Destroy(burst,1f);
        if (PlayerPrefs.GetString("PlayerShip") == "Goldy")
        {
            PlayerPrefs.SetInt("Currency", PlayerPrefs.GetInt("Currency") + CurrencyDrop + 5);
        }
        else
        {
            PlayerPrefs.SetInt("Currency", PlayerPrefs.GetInt("Currency") + CurrencyDrop);
        }
        PauseMenu.enemiesKilled++;
//		if(gameObject.tag != "HodorSpawn" && gameObject.GetComponent<orbit>()==null)
//		{
//			Debug.Log ("counter went down from AIBASE");
//			Debug.Log (gameObject.name);
//			Spawner.waveEnemies--;
//		}
            base.Die();
		
	}
}
